package com.ziyia.a3_5_all;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
    }

    public void btn(View view) throws ExecutionException, InterruptedException {
        MyHandler myHandler = new MyHandler(new WeakReference<>(this));
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Bundle bundle = new Bundle();
                bundle.putString("name", "年少有为");
                Message message = new Message();
                message.what = 1;
                message.setData(bundle);
                myHandler.sendMessage(message);
//
//                textView.setText("Aaaaaaaaa");
            }
        };


        Thread thread = new Thread(runnable);
        thread.start();
    }


    static class MyHandler extends Handler {
        WeakReference<MainActivity> activity;

        public MyHandler(WeakReference<MainActivity> activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                String name = data.getString("name");

                activity.get().textView.setText(name);


            }
        }
    }

}